package com.hcbd.tdwt.util;

import android.graphics.Color;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;
import java.util.TimeZone;


public class DateUtils {

	private static final String DATE_FORMAT_PATTERN_YMD = "yyyy-MM-dd";
	private static final String DATE_FORMAT_PATTERN_YMD_HM = "yyyy-MM-dd HH:mm";

	/**
	 * 时间戳转字符串
	 *
	 * @param timestamp     时间戳
	 * @param isPreciseTime 是否包含时分
	 * @return 格式化的日期字符串
	 */
	public static String long2Str(long timestamp, boolean isPreciseTime) {
		return long2Str(timestamp, getFormatPattern(isPreciseTime));
	}

	private static String long2Str(long timestamp, String pattern) {
		return new SimpleDateFormat(pattern, Locale.CHINA).format(new Date(timestamp));
	}

	public static String long2Str(long timestamp){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd\nHH:mm:ss");
		return sdf.format(new Date(timestamp*1000));
	}

	/**
	 * 天-》时间戳(秒)
	 *
	 * @param day 天数
	 * @return
	 */
	public static long dayToTimestamp(int day) {
		long sec = 86400;
		long timestamp = day * sec;
		return timestamp;
	}
	/**
	 * 取得当前时间戳（精确到秒）
	 *
	 * @return
	 */
	public static String getTimeStamp() {
		long time = System.currentTimeMillis();
		String timeStamp = String.valueOf(time / 1000);
		return timeStamp;
	}
	/**
	 * 时间戳转换成日期格式字符串
	 *
	 * @param seconds 精确到秒的时间戳字符串
	 * @param format
	 * @return
	 */
	public static String timeStampToDate(String seconds, String format) {
		if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
			return "";
		}
		if (format == null || format.isEmpty()) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (seconds.length() == 10) {
			return sdf.format(new Date(Long.valueOf(seconds + "000")));
		} else {
			return sdf.format(new Date(Long.valueOf(seconds)));
		}
	}
	/**
	 * 字符串转时间戳
	 *
	 * @param dateStr       日期字符串
	 * @param isPreciseTime 是否包含时分
	 * @return 时间戳
	 */
	public static long str2Long(String dateStr, boolean isPreciseTime) {
		return str2Long(dateStr, getFormatPattern(isPreciseTime));
	}

	private static long str2Long(String dateStr, String pattern) {
		try {
			return new SimpleDateFormat(pattern, Locale.CHINA).parse(dateStr).getTime();
		} catch (Throwable ignored) {
		}
		return 0;
	}
	/**
	 * 日期格式字符串转换成时间戳
	 *
	 * @param dateStr 字符串日期
	 * @param format  如：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String dateToTimeStamp(String dateStr, String format) {
		try {
			if (format == null || format.isEmpty()) {
				format = "yyyy-MM-dd HH:mm:ss";
			}
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return String.valueOf(sdf.parse(dateStr).getTime() / 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	private static String getFormatPattern(boolean showSpecificTime) {
		if (showSpecificTime) {
			return DATE_FORMAT_PATTERN_YMD_HM;
		} else {
			return DATE_FORMAT_PATTERN_YMD;
		}
	}
	public static String CalendarToString(Calendar calendar, String format) {
		if (calendar == null) {
			calendar = Calendar.getInstance();
		}

		if (format == null) {
			format = "yyyy-MM-dd HH:mm:ss";
		}

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}
	/**
	 * 
	 * @param day
	 *            天数
	 * @return
	 */
	public static String add(int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.DATE, cal.get(Calendar.DATE) - day);
		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}

	/**
	 *
	 * @param day
	 *            天数
	 * @return
	 */
	public static String add(int day, Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, cal.get(Calendar.DATE) - day);
		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}

	public static Date StringToDate(String s) {
		Date d = new Date();

		Date date;

		if (s == null)
			return null;

		d = new Date(s);

		date = new Date(d.getTime());

		return date;
	}

	public static Date StringToDate(Object o) {
		Date d = new Date();

		Date date;

		if (o == null)
			return null;

		// d = new java.util.Date("2004-01-01");

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			d = sdf.parse(o.toString());
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		date = new Date(d.getTime());

		return date;
	}

	public static int getSysYear() {
		Date d = new Date();

		Date date = new Date(d.getTime());

		return date.getYear();
	}

	public static int getSysMonth() {
		Date d = new Date();

		Date date = new Date(d.getTime());

		return date.getMonth();
	}

	public static int getSysDay() {
		Date d = new Date();

		Date date = new Date(d.getTime());

		return date.getDay();
	}

	/**
	 *
	 * @param dateTV
	 *            当前控件
	 *            标题
	 *            选中当前年
	 *            年的幅度
	 */
	public static void showFirstData(final TextView dateTV, final int days,
                                     final TextView endDateTv , final String minuteStr) {
		String year = "";
		String month = "";
		String day = "";
		String nowTime = "";
		if (!"".equals(dateTV.getText().toString().trim())) {
			String[] str = dateTV.getText().toString().trim().split("-");
			year = str[0];
			month = str[1];
			day = str[2];
		} else {
			Calendar calendar = Calendar.getInstance();
			year = calendar.get(Calendar.YEAR) + "";
			// calendar.get(Calendar.MONTH)当前月份前一个月
			month = calendar.get(Calendar.MONTH) + 1 + "";
			day = calendar.get(Calendar.DAY_OF_MONTH) + "";
			nowTime = new SimpleDateFormat("yyyy-MM-dd").format(calendar
					.getTime());
		}
		try {
			// String[] str = result.split("-");
			// dateTV.setText(str[0] + "-" + str[1] + "-"+ str[2]);
			// dateTV.setText(str[0] + "-" + str[1] + "-"+ str[2]+" 零时起");
			// dateTV.setText(nowTime+" 零时起");
			dateTV.setText(year + "-" + month + "-" + day);
			// dateTV.setText(year + "-" + month + "-"+ day+" 零时起");
			dateTV.setTextColor(Color.parseColor("#282828"));
			// 开始时间+期限天数=结束时间
			// String dateStr = str[0] + "-" + str[1] + "-"+ str[2];
			String dateStr = year + "-" + month + "-" + day;
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateStr));
			calendar.add(Calendar.DATE, days - 1);
			endDateTv.setText(new SimpleDateFormat("yyyy-MM-dd")
					.format(calendar.getTime()));
			// endDateTv.setText(new
			// SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime())+" 二十四时止");
			endDateTv.setTextColor(Color.parseColor("#282828"));
			int endInt = Integer.parseInt(endDateTv.getText().toString().trim().substring(5, 7));
			int startInt = Integer.parseInt(endDateTv.getText().toString().trim().substring(8, 10));
//			if(endInt<10){
				endDateTv.setText(endDateTv.getText().toString().trim().substring(0, 5)+endInt+endDateTv.getText().toString().trim().substring(7, 10) +minuteStr);
//				if(startInt<10){
//					endDateTv.setText(endDateTv.getText().toString().trim().substring(0, 7)+startInt  +minuteStr);
//				}
//			}else{
//				if(startInt<10){
//					endDateTv.setText(endDateTv.getText().toString().trim().substring(0, 8)+startInt  +minuteStr);
//				}
//			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}

	/**
	 *
	 * @param dateTV
	 *            当前end控件
	 */
	public static void showEndData(final TextView endDateTv, final int years, final int days,
                                   final TextView dateTV , final String minuteStr) {
		String year = "";
		String month = "";
		String day = "";
		String nowTime = "";
		if (!"".equals(endDateTv.getText().toString().trim())) {
			String[] str = endDateTv.getText().toString().trim().split("-");
			year = str[0];
			month = str[1];
			day = str[2];
		} else {
			Calendar calendar = Calendar.getInstance();
			year = calendar.get(Calendar.YEAR) + "";
			// calendar.get(Calendar.MONTH)当前月份前一个月
			month = calendar.get(Calendar.MONTH) + 1 + "";
			day = calendar.get(Calendar.DAY_OF_MONTH) + "";
			nowTime = new SimpleDateFormat("yyyy-MM-dd").format(calendar
					.getTime());
		}
		try {
			// String[] str = result.split("-");
			// dateTV.setText(str[0] + "-" + str[1] + "-"+ str[2]);
			// dateTV.setText(str[0] + "-" + str[1] + "-"+ str[2]+" 零时起");
			// dateTV.setText(nowTime+" 零时起");
			endDateTv.setText(year + "-" + month + "-" + day);
			// dateTV.setText(year + "-" + month + "-"+ day+" 零时起");
			endDateTv.setTextColor(Color.parseColor("#282828"));
			// 开始时间+期限天数=结束时间
			// String dateStr = str[0] + "-" + str[1] + "-"+ str[2];
			String dateStr = year + "-" + month + "-" + day;
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateStr));
			if("01".equals(month)&&"01".equals(day)){
				calendar.add(Calendar.DATE, days+1);
			}else{
				calendar.add(Calendar.DATE, days+1);
			}
			calendar.add(Calendar.YEAR, years- 1);
			dateTV.setText(new SimpleDateFormat("yyyy-MM-dd")
					.format(calendar.getTime()));
			// endDateTv.setText(new
			// SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime())+" 二十四时止");
			dateTV.setTextColor(Color.parseColor("#282828"));
			int endInt = Integer.parseInt(dateTV.getText().toString().trim().substring(5, 7));
			int startInt = Integer.parseInt(dateTV.getText().toString().trim().substring(8, 10));
//			if(endInt<10){
				dateTV.setText(dateTV.getText().toString().trim().substring(0, 5)+endInt+dateTV.getText().toString().trim().substring(7, 10) +minuteStr);
//				if(startInt<10){
//					dateTV.setText(dateTV.getText().toString().trim().substring(0, 7)+startInt +minuteStr);
//				}
//			}else{
//				if(startInt<10){
//					dateTV.setText(dateTV.getText().toString().trim().substring(0, 8)+startInt +minuteStr);
//				}
//			}

		} catch (ParseException e) {
			e.printStackTrace();
		}

	}
	public static String getSysDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}

	public static String getSmallSysDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(new Date());
	}
	public static String getSysYearMothDay() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date());
	}
	public static String getTimeLong(String endTime) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			Date now;

			now = df.parse(getSysDate());

			Date date = df.parse(endTime);

			long l = now.getTime() - date.getTime();
			long day = l / (24 * 60 * 60 * 1000);
			long hour = (l / (60 * 60 * 1000) - day * 24);
			long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);

			String str = "";
			double result = ((((day * 24) + hour) * 60) + min);
			if (result < 60) {
				str = result + "分钟";
			} else {
				DecimalFormat df2 = new DecimalFormat("###.0");

				str = df2.format(result / 60.0) + "小时";
			}
			return str;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 取得当前时间的日期字符串
	 *
	 * @returnt
	 */
	public static String getDateNow() {
		Calendar cal = Calendar.getInstance();
		String retStr = "";
		retStr = cal.get(cal.YEAR) + "-" + (cal.get(cal.MONTH) + 1) + "-"
				+ cal.get(cal.DAY_OF_MONTH);
		return retStr;
	}

	public static String getDateSerial() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSSSS");
		return sdf.format(new Date());
	}

	public static String getDateShortSerial() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
		return sdf.format(new Date());
	}

	public static String getDateLongSerial() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}

	public static String getDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date());
	}

	public static long turnBDTimeToDate(String BDTime) {
		long timestamp = 0;
		try{
//			String inputTime = "070723.00";
			String inputTime = BDTime;
			// 获取当前日期的年、月、日
			LocalDateTime now = LocalDateTime.now();
			int year = now.getYear();
			int month = now.getMonthValue();
			int day = now.getDayOfMonth();
			// 从输入字符串中提取小时、分钟和秒
			int hour = Integer.parseInt(inputTime.substring(0, 2));
			int minute = Integer.parseInt(inputTime.substring(2, 4));
			int second = Integer.parseInt(inputTime.substring(4, 6));

			// 加上时区偏移量（+8小时）
//			hour += 8;
//			if (hour >= 24) {
//				hour -= 24; // 如果超过一天，则小时数减去24
//				day += 1;   // 并将日期加一天
//			}
			// 构造日期时间对象
			LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute, second);
			// 转换为时间戳（以秒为单位）
			timestamp = dateTime.toEpochSecond(ZoneOffset.UTC);
		}catch (Exception e){
			e.printStackTrace();
		}
		return timestamp;
	}
	/**
	 * 判断当前时间是否大于某个时点
	 * @param time HH:mm:ss
	 * @return
	 */
	public static boolean timeCompare(String time){

		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
		LocalTime localTime=LocalTime.parse(time,dtf);
		return LocalTime.now().isAfter(localTime);
	}
	public static String getDateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(new Date());
	}
	public static String getDateTOString(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

	public static String getDateYear(String ds) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date d = new Date();
		try {
			d = sdf.parse(ds);
			calendar.setTime(d);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return String.valueOf(calendar.get(Calendar.YEAR));
	}

	public static String getDateMonth(String ds) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new Date();
		try {
			d = sdf.parse(ds);
			calendar.setTime(d);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return String.valueOf(calendar.get(Calendar.MONTH) + 1);
	}

	public static String getDateDay(String ds) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new Date();
		try {
			d = sdf.parse(ds);
			calendar.setTime(d);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
	}

	public static String getDateHour(String ds) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
		Date d = new Date();
		try {
			d = sdf.parse(ds);
			calendar.setTime(d);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
	}

	public static String formatDateTime(String ds) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat sdf2 = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date d = new Date();
		String df = null;
		try {
			d = sdf2.parse(ds);
			df = sdf.format(d);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return df;
	}

	public static String formatDate(String ds) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new Date();
		String df = null;

		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
		try {
			d = sdf2.parse(ds);
			df = sdf.format(d);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return df;
	}

	public static String formatDate() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date d = new Date();
		String df = null;

		df = sdf.format(d);

		return df;
	}

	public static String formatDateYM() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		Date d = new Date();
		String df = null;

		df = sdf.format(d);

		return df;
	}

	public static String formatShortDateTime(String ds) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
		SimpleDateFormat sdf2 = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date d = new Date();
		String df = null;
		try {
			d = sdf2.parse(ds);
			df = sdf.format(d);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return df;
	}

	/**
	 * 添加(或减小)时间
	 *
	 * @param date
	 * @param field
	 *            要添加(或减小)的字段(年或月或日或...)
	 * @param amount
	 *            要添加(或减小)的数量，amount为正数时，是添加，为负数时是减小
	 * @return 日期
	 */
	public static Date add(Date date, int field, int amount) {
		Date uDate = convert2JavaDate(date);
		Calendar ca = Calendar.getInstance();
		ca.setTime(uDate);
		ca.add(field, amount);
		Date uNewDate = ca.getTime();
		Date newDate = convert2SqlDate(uNewDate);
		return newDate;
	}

	/**
	 * 添加(或减小)时间
	 *
	 * @param date
	 * @param field
	 *            要添加(或减小)的字段(年或月或日或...)
	 * @param amount
	 *            要添加(或减小)的数量，amount为正数时，是添加，为负数时是减小
	 * @param pattern
	 *            格式化模式
	 * @return 格式化后的日期字符串
	 */
	public static String add(Date date, int field, int amount, String pattern) {

		Date uDate = convert2JavaDate(date);
		Calendar ca = Calendar.getInstance();
		ca.setTime(uDate);
		ca.add(field, amount);

		return format(ca.getTime(), pattern);
	}

	/**
	 * 添加(或减小)时间
	 *
	 * @param date
	 * @param field
	 *            要添加(或减小)的字段(年或月或日或...)
	 * @param amount
	 *            要添加(或减小)的数量，amount为正数时，是添加，为负数时是减小
	 * @param pattern
	 *            格式化模式
	 * @return 格式化后的日期字符串
	 * @throws ParseException
	 */
	public static String add(String date, int field, int amount, String pattern)
			throws ParseException {
		Calendar ca = Calendar.getInstance();
		ca.setTime(parse(date, pattern));
		ca.add(field, amount);

		return format(ca.getTime(), pattern);
	}

	/**
	 * 根据字符串生成日期
	 *
	 * @param dateStr
	 * @param pattern
	 * @return Date
	 * @throws ParseException
	 */

	public static Date parse(String dateStr, String pattern)
			throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.parse(dateStr);
	}

	/**
	 * 格式化日期
	 *
	 * @param date
	 * @param pattern
	 * @return String
	 */
	public static String format(Date date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	/**
	 * 将java.util.Date转换为java.sql.Date
	 *
	 * @param javaDate
	 * @return
	 */
	public static Date convert2SqlDate(Date javaDate) {
		Date sd;

		sd = new Date(javaDate.getTime());
		return sd;

	}

	/**
	 * 将java.sql.Date转换为java.util.Date
	 *
	 * @param sqlDate
	 * @return
	 */
	public static Date convert2JavaDate(Date sqlDate) {
		return new Date(sqlDate.getTime());
	}






	/**  * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss  *   * @param dateDate  * @return  */
	public static String getNormalTime(long value)
	{
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
	    String time = format.format(new Date(value)) ;
	    return time;
	}

	/**  * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss  *   * @param dateDate  * @return  */
	public static String getNormaDate(long value)
	{
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd") ;
	    String time = format.format(new Date(value)) ;
	    return time;
	}


	public static String getStringDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(new Date());
	}

	public static String getDateShortSerial2() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSS");
		return sdf.format(new Date());
	}


	/**
	 *
	 * @param startData
	 *            开始时间
	 * @param endData
	 *            结束时间
	 * @return
	 */
	public static int dataContrast(String startData, String endData) {
		java.text.DateFormat df = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(df.parse(startData));
			c2.setTime(df.parse(endData));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int result = c1.compareTo(c2);
		if (result == 0) {
			return -1;
		} else if (result < 0) {
			return 1;
		} else {
			return 0;
		}
	}

    /**
     * 通过年份和月份 得到当月的日子
     */
    public static int getMonthDays(int year, int month) {
        month++;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
                    return 29;
                }else{
                    return 28;
                }
            default:
                return  -1;
        }
    }

    /**
     * 返回当前月份1号位于周几
     * @param year
     * 		年份
     * @param month
     * 		月份，传入系统获取的，不需要正常的
     * @return
     * 	日：1		一：2		二：3		三：4		四：5		五：6		六：7
     */
    public static int getFirstDayWeek(int year, int month){
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }
    /**
	 * 求两个时间差
	 * @param startData
	 *            开始时间
	 * @param endData
	 *            结束时间
	 * @return
	 */
	public static double timeReduce(String startData, String endData) {
		java.text.DateFormat df = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		double reduce = 0;
		try {
			Date start = df.parse(startData);
			Date end = df.parse(endData);
			double l = end.getTime()-start.getTime();
			double day = l / (24 * 60 * 60 * 1000.0);
			double hour = (l / (60 * 60 * 1000) - day * 24.0);
			double min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60.0);
		    reduce = ((((day * 24) + hour) * 60.0) + min);
		
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return reduce;
	}

	/**校验时期是否合法**/
	public static  boolean checkDate(String dateStr){
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			format.setLenient(false);
			format.parse(dateStr);
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}
	//获取月日
	//day:+1明天 -1前天

     public static String getStringMonthDay(int month, int day){
		 Calendar calendar = Calendar.getInstance();
		 calendar.add(Calendar.DAY_OF_MONTH,day);
		 calendar.add(Calendar.MONTH,month);
		 int  numberOfdays= calendar.get(Calendar.MONTH)+1;
		 int  months = calendar.get(Calendar.DAY_OF_MONTH);
		 return numberOfdays+"月"+months+"日";
	 }
	public static int yearMothDayContrast(String startDay, String endDay) {
		java.text.DateFormat df = new SimpleDateFormat(
				"yyyy-MM-dd");
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(df.parse(startDay));
			c2.setTime(df.parse(endDay));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int result = c1.compareTo(c2);
		if (result == 0) {
			return -1;
		} else if (result < 0) {
			return 1;
		} else {
			return 0;
		}
	}
	public static int yearMothDayTimeContrast(String startDay, String endDay) {

		java.text.DateFormat df = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm");
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(df.parse(startDay));
			c2.setTime(df.parse(endDay));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int result = c1.compareTo(c2);
		if (result == 0) {
			return -1;
		} else if (result < 0) {
			return 1;
		} else {
			return 0;
		}
	}
	//简化时间
	// 2024-08-24 14:07:10
	public static String simplifyTime(String date) {
		if("".equals(date)){
			return "";
		}
		try {
			int yearIndex = date.indexOf(" ");
			String year = date.substring(0, yearIndex);
			int days = daysBetween(year, getSysYearMothDay());
			String slot = "";
			if (days == 0) {
				String hour = date.substring(yearIndex + 1, date.length() - 6);
				if (Integer.parseInt(hour) < 4) {
					slot = "凌晨";
				} else if ((Integer.parseInt(hour) < 7)) {
					slot = "早上";
				} else if ((Integer.parseInt(hour) < 10)) {
					slot = "上午";
				} else if ((Integer.parseInt(hour) < 12)) {
					slot = "中午";
				} else if ((Integer.parseInt(hour) < 16)) {
					slot = "下午";
				} else if ((Integer.parseInt(hour) < 18)) {
					slot = "下午";
				} else if ((Integer.parseInt(hour) < 22)) {
					slot = "晚上";
				}
				return slot + date.substring(yearIndex + 1, date.length() - 3);
			} else if (days == 1) {
				slot = "昨天";
				return slot + date.substring(yearIndex + 1, date.length() - 3);
			} else if (days == 2) {
				slot = "前天";
				return slot + date.substring(yearIndex + 1, date.length() - 3);
			} else {
				return date.substring(0, date.indexOf(" ") + 1);
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return date;
	}
	public static int daysBetween(Date smdate, Date bdate) throws ParseException
	{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		smdate=sdf.parse(sdf.format(smdate));
		bdate=sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days=(time2-time1)/(1000*3600*24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 *字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate, String bdate){
		int day=0;
		try {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days=(time2-time1)/(1000*3600*24);


			day= Integer.parseInt(String.valueOf(between_days));
		}catch (Exception e){
			e.printStackTrace();
		}
		return day;
	}

	public static String displayMonth(String time){
		if(time==null||time==""){
			return "";
		}
		String timeTmp=time.substring(time.indexOf("-")+1);
		return timeTmp.substring(0,timeTmp.lastIndexOf(":"));
	}
	public static String stringForTime(int timeMs) {
		StringBuilder mFormatBuilder = new StringBuilder();
		Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
	//	int totalSeconds = timeMs / 1000;
		int seconds = timeMs % 60;
		int minutes = (seconds / 60) % 60;
		int hours = seconds / 3600;
		mFormatBuilder.setLength(0);
		if (hours > 0) {
			return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
		} else {
			return mFormatter.format("%02d:%02d", minutes, seconds).toString();
		}
	}
	public static String secToTime(int time) {
		String timeStr = null;
		int hour = 0;
		int minute = 0;
		int second = 0;
		if (time <= 0)
			return "00:00";
		else {
			minute = time / 60;
			if (minute < 60) {
				second = time % 60;
				timeStr = unitFormat(minute) + ":" + unitFormat(second);
			} else {
				hour = minute / 60;
				if (hour > 99)
					return "99:59:59";
				minute = minute % 60;
				second = time - hour * 3600 - minute * 60;
				timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
			}
		}
		return timeStr;
	}

	public static String unitFormat(int i) {
		String retStr = null;
		if (i >= 0 && i < 10)
			retStr = "0" + Integer.toString(i);
		else
			retStr = "" + i;
		return retStr;
	}



}
